﻿namespace Hotel.Web.Controllers
{
    using System;
    using System.Web.Mvc;
    using Hotel.Web.Models;
    using System.Web.Security;

    /// <summary>
    /// Controller for admin create/delete new users
    /// </summary>
    public class AdministratorController : Controller
    {
        //
        // GET: /Administrator/
        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Index(Admin model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //RoleProvider.
                    if (Membership.FindUsersByName(model.Login).Count < 1)
                    {
                        Membership.CreateUser(model.Login, model.Password);
                    }
                    Roles.AddUserToRole(model.Login, model.SelectedRole.ToString());
                    return View("Success");
                }
                catch (Exception ex)
                {
                    Error err = new Error();
                    err.ErrorMessage = ex.Message;
                    return View("Error", err);
                }
            }
            return View();
        }

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(Admin model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //RoleProvider.     
                    Roles.RemoveUserFromRole(model.Login, model.SelectedRole.ToString());
                    Membership.DeleteUser(model.Login);
                    return View("Success");
                }
                catch (Exception ex)
                {
                    Error err = new Error();
                    err.ErrorMessage = ex.Message;
                    return View("Error", err);
                }
            }
            return View();
        }
    }
}
